(function (root, factory) {
    'use strict'

    if (typeof define === 'function' && define.amd) {
        //AMD. Register as an anonymous modules
        define([], factory);
    } else if (typeof exports === 'object') {
        //COMMONJS
        module.exports = factory();
    } else {
        //Browser
        root.Ld = factory(root);
    }
}(this, function () {
    'use strict'

    var defaults = {
        canvas: 'canvas' //id
    }

    var extend = function () {
        var settings = {};
        var length = arguments.length;

        function merge(obj) {
            for (var prop in obj) {
                if (obj.hasOwnProperty(prop)) {
                    settings[prop] = obj[prop];
                }
            }
        }

        for (var i = 0; i < length; i++) {
            var obj = arguments[i];
            merge(obj);
        }

        return settings;
    }

    function LookDog() {
        this.canvas = "";
        this.ctx = "";
        this.num = 0; //找的次数
        this.dx = 90; //图片x轴间隔距离
        this.dy = 90; //图片y轴间隔距离
        this.countTime = 60; //倒计时
        this.timer = null;
        this.lovers = [];
        this.dog = null;
    }

    LookDog.prototype = {
        pageSize: {
            width: 0,
            height: 0
        },
        imgSize: {
            width: 98,
            height: 98
        },
        init: function (options) {
            var that = this;
            this.sets = extend(defaults, options || {});
            this.canvas = document.getElementById(this.sets.canvas);
            document.getElementById('time').innerHTML = this.countTime + "s";
            if (this.canvas === "undefind") {
                return false;
            }
            this.ctx = this.canvas.getContext('2d');
            this.countDown();
            this.setPageSize();
            this.initImage();
            this.drawCanvas();
            this.canvas.addEventListener('touchstart', that.touchStart.bind(that), false);
        },
        countDown: function () {
            this.timer = setTimeout(() => {
                this.countTime--;
                document.getElementById('time').innerHTML = this.countTime + "s";
                this.timer = null;
                if (this.countTime <= 0) {
                    this.endGame();
                } else {
                    this.countDown();
                }
            }, 1000)
        },
        setPageSize: function () {
            this.pageSize.width = document.body.clientWidth;
            this.pageSize.height = document.body.clientHeight;
            this.canvas.width = this.pageSize.width;
            this.canvas.height = this.pageSize.height;
            //console.log(this.pageSize);
        },
        initImage: function () {
            this.lovers = document.getElementsByClassName('lover');
            this.dog = document.getElementById('dog');
            //console.log(this.lovers);
        },
        touchStart: function (e) {
            var x = e.touches[0].pageX;
            var y = e.touches[0].pageY;
            this.verify(x, y);
        },
        drawCanvas: function () {
            var lengthX = Math.floor(this.pageSize.width / this.dx);
            var lengthY = Math.floor(this.pageSize.height / this.dy);
            var dogX = Math.floor(Math.random() * (lengthX - 4) + 2);
            var dogY = Math.floor(Math.random() * (lengthY - 4) + 2);

            for (var i = 0; i < lengthX; i++) {
                for (var j = 0; j < lengthY; j++) {
                    var roundX = Math.random() * 40 - 20;
                    var roundY = Math.random() * 60 - 30;
                    if (dogX == i && dogY == j) {
                        this.ctx.drawImage(this.dog, i * this.dx + roundX, j * this.dy + roundY, this.imgSize.width, this.imgSize.height);
                        //狗的位置,用于点击判断
                        this.dogPosition = {
                            dogX: i * this.dx + roundX,
                            dogY: j * this.dy + roundY
                        };
                    } else {
                        var randomNum = Math.floor(Math.random() * this.lovers.length);
                        this.ctx.drawImage(this.lovers[randomNum], i * this.dx + roundX, j * this.dy + roundY, this.imgSize.width, this.imgSize.height);
                    }
                }
            }
        },
        verify: function (x, y) {
            if ((x >= this.dogPosition.dogX && x < (this.dogPosition.dogX + this.imgSize.width)) && (y >= this.dogPosition.dogY && y < (this.dogPosition.dogY + this.imgSize.height))) {
                //成功提醒

                //分数+1
                this.num++;
                this.dx -= 3;
                this.dy -= 3;
                this.drawCircle();
                //console.log(this.num);
                //清除画布
                setTimeout(() => {
                    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
                    this.drawCanvas();
                }, 600)
            }
        },
        drawCircle: function () {
            this.ctx.beginPath();
            this.ctx.strokeStyle = "#fff";
            this.ctx.lineWidth = "4";
            this.ctx.arc((this.dogPosition.dogX + this.imgSize.width / 2), (this.dogPosition.dogY + this.imgSize.height / 2), this.imgSize.width / 2, 0, 2 * Math.PI);
            this.ctx.stroke();
        },
        endGame: function () {
            //结束游戏跳转
            //window.location.href = "end.html?score="+this.num;
            document.getElementById('index').style.display = 'none';
            document.getElementById('end').style.display = 'block';
            document.getElementById('score').innerHTML = (this.num || '0') + " 分";
            console.log("游戏结束");
        }
    };

    return LookDog;
}))